// const { read } = require("fs");
var fs = require("fs");
var http = require("http");
var path = require("path");

// 客户端向端口8081发请求，执行回调函数，服务端收到请求后响应
http.createServer((req, res) => {
              /* 状态码200，正常响应  ;  响应返回html*/
    res.writeHead(200, {"Content-Type": "text/html"});
    
    var filePath = path.join(__dirname,"/book.jpg");
    var imgBuf = fs.readFileSync(filePath); //获取图片的buffer数据
    var imgBase64 = imgBuf.toString("base64"); //转换为base64数据

    res.write("<!DOCTPYE html><html>" +
        "<head></head><body'>" + 
        "<img src='data: image/jpg; base64, "+
            imgBase64 +
        " ' />" +
        "</body></html>"
    );
    res.end();

}).listen(8081);

console.log("server is listening 8081");